<template>
  <div class="mod-user">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="card-font">穿越火线S16赛季枪王排位前百排名</span>
      </div>
      <el-table :data="topTenDataList" style="width: 100%" :row-class-name="tableRowClassName">
        <el-table-column prop="rank" label="排行" width="150px" align="center">
        </el-table-column>
        <el-table-column prop="nicename" label="昵称" align="center">
        </el-table-column>
        <el-table-column prop="qqAccount" label="QQ账号" align="center">
        </el-table-column>
        <el-table-column prop="score" label="点数" align="center">
        </el-table-column>
      </el-table>
    </el-card>
    <el-form :inline="true" :model="dataForm" class="demo-form-inline" style=" margin-top: 23px">
      <el-form-item>
        <el-input v-model="dataForm.nicename" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="success" @click="getDataList()">查询</el-button>
        <el-button type="info" @click="exportExcel()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="rank" label="排行" width="150" align="center">
      </el-table-column>
      <el-table-column prop="nicename" label="昵称" align="center">
      </el-table-column>
      <el-table-column prop="qqAccount" label="QQ账号" align="center">
      </el-table-column>
      <el-table-column prop="score" label="点数" align="center">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.gamePlayerId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.gamePlayerId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="flushDataList()"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update';
  export default {
    data() {
      return {
        addOrUpdateVisible: false,
        dataForm: {
          user: '',
          region: ''
        },
        dataList: [],
        topTenDataList: [],
        dataForm: {
          nicename: ''
        },
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      //获取数据列表
      this.getDataList()
      //获取前十数据列表
      this.gettopTenDataList()
    },
    methods: {
      onSubmit() {
        console.log('submit!');
      },
      tableRowClassName({
        row,
        rowIndex
      }) {
        if (rowIndex % 2 == 1) {
          return 'warning-row';
        } else {
          return 'success-row';
        }
        return '';
      },
      // 修改
      addOrUpdateHandle(gamePlayerId) {
        console.log("come in")
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          console.log("中间")
          this.$refs.addOrUpdate.init(gamePlayerId)
        })
      },
       //导出
        exportExcel() {
          this.$http({
          url: this.$http.adornUrl('/rank/rankbyredis/export/gamePlayer'),
          method: 'get',
        }).then(({
          data
        }) => {
          let a = document.createElement('a')
                    a.href = this.$https.state.baseURL + res.data.data
                    a.click();
        })
        },
      //刷新列表
      flushDataList(){
        this.gettopTenDataList()
        this.getDataList()
      },
      // 获取前十数据列表
      gettopTenDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/rank/rankbyredis/topTenList'),
          method: 'get',
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.topTenDataList = data.topTenList
          } else {
            this.topTenDataList = []
          }
          this.dataListLoading = false
        })
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/rank/rankbyredis/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'nicename': this.dataForm.nicename
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.list 
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
            // 删除
      deleteHandle(gamePlayerId) {
        this.$confirm(`确定要删除该数据?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/rank/rankbyredis/delete/${gamePlayerId}`),
            method: "delete",
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.getDataList();
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.flushDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      }
    }
  }

</script>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .card-font {
    margin-left: 33%;
    font-size: xx-large;
    color: chocolate;
    font-weight: bolder;
  }

</style>
